//
//  SetPswViewController.m
//  DaJiang
//
//  Created by sally on 16/10/9.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CellManager.h"
#import "SetPswViewController.h"
#import "UserModel.h"

#define TextSetPsw @"设置密码"
#define TextPhone @"手机号"
#define TextOldPsw @"原始密码"
#define TextNewPsw @"新密码"
#define TextRePsw @"确认密码"

@interface SetPswViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *tableData;
@property (strong, nonatomic) UIImageView *pswRightView;

@end

@implementation SetPswViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"设置密码";
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = @[ TextOldPsw, TextNewPsw, TextRePsw ];
}

#pragma mark - action
#pragma mark 点击提交
- (IBAction)actionOnSend:(id)sender {
  NSIndexPath *path00 = [NSIndexPath indexPathForRow:0 inSection:0];
  UITableViewCell *cell00 = [self.tableView cellForRowAtIndexPath:path00];
  UITextField *textField00 = (UITextField *) [cell00.contentView.subviews lastObject];
  NSIndexPath *path01 = [NSIndexPath indexPathForRow:1 inSection:0];
  UITableViewCell *cell01 = [self.tableView cellForRowAtIndexPath:path01];
  UITextField *textField01 = (UITextField *) [cell01.contentView.subviews lastObject];
  NSIndexPath *path02 = [NSIndexPath indexPathForRow:2 inSection:0];
  UITableViewCell *cell02 = [self.tableView cellForRowAtIndexPath:path02];
  UITextField *textField02 = (UITextField *) [cell02.contentView.subviews lastObject];
  if (textField00.text.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入原始密码"];
    return;
  }
  if (textField01.text.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入新密码"];
    return;
  }
  if (textField02.text.length < 1) {
    [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入确认密码"];
    return;
  }
  
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod]createProgressHUDInView:self.view withMessage:@""];
  
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  [UserModel modifyPswWithPhone:userModel.user_data.phone oldPassword:textField00.text password:textField01.text confirmPwd:textField02.text completion:^(BOOL isSucceed) {
    if (isSucceed) {
      [[CommonMethod sharedCommonMethod] showAlertMessage:@"设置密码成功"];
      [self.tableView reloadData];
    } else {
      NSError *error = [UserModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;
  }];
  
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.tableData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

  //主标题
  [CellManager configCellNormalText:cell];
  cell.textLabel.text = self.tableData[indexPath.row];

  //编辑行
  UITextField *textField = [CellManager configCellNormalTextField:cell];
  textField.delegate = self;
  textField.tag = indexPath.row;
  textField.secureTextEntry = YES;
  [cell.contentView addSubview:textField];

  //可视控件
  UIImageView *pswRightView = [[UIImageView alloc] init];
  pswRightView.size = CGSizeMake(40, 40);
  pswRightView.contentMode = UIViewContentModeCenter;
  pswRightView.image = [UIImage imageNamed:@"psw_invisible_ic"];
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionPswVisible:)];
  textField.rightView = pswRightView;
  textField.rightViewMode = UITextFieldViewModeAlways;
  textField.rightView.userInteractionEnabled = YES;
  textField.rightView.tag = indexPath.row;
  [textField.rightView addGestureRecognizer:tap];

  if (indexPath.row == 0) {
    textField.placeholder = @"请输入原始密码";
  } else if (indexPath.row == 1) {
    textField.placeholder = @"请输入6-12位数字或字母密码";
  }

  return cell;
}

#pragma mark - 私有方法
#pragma mark 操作密码可视化控件
- (void)actionPswVisible:(UITapGestureRecognizer *)recognizer {
  UIImageView *pswRightView = (UIImageView *) recognizer.view;
  NSIndexPath *path = [NSIndexPath indexPathForRow:pswRightView.tag inSection:0];
  UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path];
  UITextField *textField = (UITextField *) [cell.contentView.subviews lastObject];

  if (textField.isSecureTextEntry) {
    pswRightView.image = [UIImage imageNamed:@"psw_visible_ic"];
  } else {
    pswRightView.image = [UIImage imageNamed:@"psw_invisible_ic"];
  }
  textField.secureTextEntry = !textField.isSecureTextEntry;
}

@end
